package com.lewis.sync.entity;


import com.lewis.sync.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 连接信息实体
 *
 * @author Lewis
 */
@Slf4j
public class ConnInfo {

	/**
	 * 连接对象 a
	 */
	public Connection aConn = null;
	public String aDriver = "";
	public String aUrl = "";
	public String aUser = "";
	public String aPassword = "";

	/**
	 * 连接对象 b
	 */
	public Connection bConn = null;
	public String bDrive = "";
	public String bUrl = "";
	public String bUser = "";
	public String bPassword = "";


	/**
	 * 构造器
	 */
	public ConnInfo() {

	}

	/**
	 * 创建一个连接对象 a
	 *
	 * @return Connection
	 */
	public Connection aConnCreate() {
		try {
			aDriver = PropertiesUtils.getValue("a-driver");
			aUrl = PropertiesUtils.getValue("a-url");
			aUser = PropertiesUtils.getValue("a-user");
			aPassword = PropertiesUtils.getValue("a-password");
			Class.forName(aDriver);
			this.aConn = DriverManager.getConnection(aUrl, aUser, aPassword);
		} catch (Exception e) {
			log.error("初始化连接对象a异常", e);
		}
		return this.aConn;
	}

	/**
	 * 创建一个连接对象b
	 *
	 * @return Connection
	 */
	public Connection bConnCreate() {
		try {
			bDrive = PropertiesUtils.getValue("b-driver");
			bUrl = PropertiesUtils.getValue("b-url");
			bUser = PropertiesUtils.getValue("b-user");
			bPassword = PropertiesUtils.getValue("b-password");
			Class.forName(bDrive);
			this.bConn = DriverManager.getConnection(bUrl, bUser, bPassword);
			this.bConn.setAutoCommit(false);
		} catch (Exception e) {
			log.error("初始化连接对象b异常", e);
		}
		return this.bConn;
	}
}